function onClickLogin(eventObject) {
    $('#usernameField').parent().removeClass('has-error has-feedback').find('span').remove();
    $('#passwordField').parent().removeClass('has-error has-feedback').find('span').remove();

    var username = $('#usernameField').val();
    if (!username) {
        $('#usernameField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Username must have 1-70 characters!</span>').insertAfter('#usernameField');
        return;
    }
    if (username.length > 70) {
        $('#usernameField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Username must have 1-70 characters!</span>').insertAfter('#usernameField');
        return;
    }

    var password = $('#passwordField').val();
    if (!password) {
        $('#passwordField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Password must have 1-70 characters!</span>').insertAfter('#passwordField');
        return;
    }
    if (password.length > 70) {
        $('#passwordField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Password must have 1-70 characters!</span>').insertAfter('#passwordField');
        return;
    }

    sendLogin(username, password, function (data) {
        refreshNavBar();
    });
}

function onClickLogOut(eventObject) {
    sendLogout(function (data) {
        refreshNavBar();
        location.href = '/Trabalho_PD/homepage.html';
    });
}

function onClickSucess(data, textStatus, jqXHR) {
    $("#mynavbar").replaceWith(data);
}

function onClickError(jqXHR, textStatus, errorThrown) {
}

function onClickUserAccountConfiguration(eventObject) {
    location.href = '/Trabalho_PD/useraccountconfiguration.html';
}

function onClickMyTrips(eventObject) {
    location.href = '/Trabalho_PD/mytrips.html';
}

function onClickServerConfiguration(eventObject) {
    location.href = '/Trabalho_PD/serverconfigurations.html';
}

function onClickAdminAccountConfiguration(eventObject) {
    location.href = '/Trabalho_PD/adminconfigurations.html';
}

function onClickForum(eventObject) {
    location.href = '/Trabalho_PD/forum.html';
}

function onClickEmail(eventObject) {
    location.href = '/Trabalho_PD/email.html';
}

function onClickAccounts(eventObject) {
    location.href = '/Trabalho_PD/accounts.html';
}

function onClickCars(eventObject) {
    location.href = '/Trabalho_PD/seecars.html';
}

function onClickUnlockAccount(eventObject) {
    sendRequestCancelledAccountReactivation(function(data) {
        if(data["success"]) {
            refreshNavBar();
        }
    });
}

function refreshNavBar() {
    var url = "/Trabalho_PD/homepage/navbar";
    $.ajax({
        type: "GET",
        dataType: "html",
        url: url,
        cache: false,
        success: onClickSucess,
        error: onClickError
    });
}

$(document).ready(function () {
    refreshNavBar();
});